package com.ruoyi.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Yuan
 * @description
 * @date 2024/1/25
 */
public class SequenceUtil {
    /**
     * redis流水号工具类
     */
    private static final int DEFAULT_LENGTH = 3;

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    /**
     * 将传入的数 seq 格式化成 length 位，不够前边补 0
     * 如果 length < 3 则按照 3 算
     *
     * @param str
     * @param length
     * @return
     */
    public static String getSequence(String str, int length) {
        int len = str.length();
        length = Math.max(length, DEFAULT_LENGTH);
        if (len >= length) {
            return str;
        }
        int rest = length - len;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rest; i++) {
            sb.append('0');
        }
        sb.append(str);
        return sb.toString();
    }

    // 获取第二天00:00的时间
    public static Date getMidnightDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /***
     * 获取某一时间 所在 月份 下个月的第一天
     *  比如 ： 2020-01-20
     *     结果为：2020-02-01 00:00:00
     */
    public static Date getFirstDayOfNext(Date date) {
        Calendar nextMonthFirst = Calendar.getInstance();
        nextMonthFirst.setTime(date);
        nextMonthFirst.set(Calendar.DAY_OF_MONTH, 1);
        nextMonthFirst.add(Calendar.MONTH, 1);
        return nextMonthFirst.getTime();
    }
}

